package com.yonyou.pmclouds.plan.planindex.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertMultiProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.template.MultiInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.plan.planindex.entity.PlanIndexVO;
import com.yonyou.pmclouds.plan.planindex.mapper.PlanIndexMapper;
import com.yonyou.pmclouds.plan.planindex.rmiitf.IPlanIndexMaintain;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author th
 * @date 2019/11/14 10:10
 */
@Service(interfaceClass = IPlanIndexMaintain.class, version = ApplicationConsts.APPLICATION_VERSION)
public class PlanIndexMaintainImpl implements IPlanIndexMaintain {
    private static final String[] UPDATE_FIELDS = {PlanIndexVO.EXPECT_TOTAL};

    @Autowired
    private PlanIndexMapper mapper;

    @Override
    public List<PlanIndexVO> save(List<PlanIndexVO> indexVOLIst, List<String> deleteIndexes) throws BusinessException {
        if (CollectionUtils.isEmpty(indexVOLIst))
            return new ArrayList<>(0);

        // 删除计划指标
        this.delete(deleteIndexes);

        // 新增，更新的集合
        List<PlanIndexVO> insertList = new ArrayList<>();
        List<PlanIndexVO> updateList = new ArrayList<>();
        for (PlanIndexVO vo : indexVOLIst) {
            if (StringUtils.isEmpty(vo.getPkPlanIndex())) {
                insertList.add(vo);
            } else {
                updateList.add(vo);
            }
        }

        if (insertList.size() > 0) {
            MultiInsertBpTemplate<PlanIndexVO> bp = new MultiInsertBpTemplate<>(mapper);
            bp.addBeforeProcessor(new AuditInfoInsertMultiProcessor<PlanIndexVO>());
            bp.insert(insertList.toArray(new PlanIndexVO[0]));
        }

        if (updateList.size() > 0) {
            SingleUpdateBpTemplate<PlanIndexVO> bpTemplate = new SingleUpdateBpTemplate<>(mapper, UPDATE_FIELDS);
            bpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<PlanIndexVO>());
            for (PlanIndexVO updateVO : updateList) {
                bpTemplate.update(updateVO);
            }
        }
        return indexVOLIst;
    }

    @Override
    public void delete(List<String> pkPlanIndexList) throws BusinessException {
        if (CollectionUtils.isEmpty(pkPlanIndexList))
            return;

        List<PlanIndexVO> deleteVOs = mapper.queryByPks(pkPlanIndexList);
        if (CollectionUtils.isEmpty(deleteVOs))
            return;

        SingleDeleteBpTemplate<PlanIndexVO> bpTemplate = new SingleDeleteBpTemplate<>(mapper);
        bpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<PlanIndexVO>());
        for (PlanIndexVO vo : deleteVOs) {
            bpTemplate.delete(vo);
        }
    }

    @Override
    public PlanIndexVO[] insert(PlanIndexVO[] indexVOS) throws BusinessException {
        return insert(indexVOS, true);
    }

    @Override
    public PlanIndexVO[] insert(PlanIndexVO[] indexVOS, boolean isGenId) throws BusinessException {
        MultiInsertBpTemplate<PlanIndexVO> bp = new MultiInsertBpTemplate<>(mapper,isGenId);
        bp.addBeforeProcessor(new AuditInfoInsertMultiProcessor<PlanIndexVO>());
        return bp.insert(indexVOS);
    }
}
